package com.gaara.test;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;

import com.gaara.ddp.R;

public class StartScene extends Scene{

	private Bitmap background;

	private Bitmap[] titleSnow;
	private int snowIndex = 0;
	private float angle = 0f;
	private int alpha = 0;
	private int alphaAdd = 1;
	private float scale = 0.5f;

	//气球动画
	private Ball[] balls = new Ball[5];

	//开始按钮 
	private MyButton startBtn;

	public StartScene(MyView view) {
		super(view);
	}


	public Bitmap getBackground() {
		return background;
	}



	@Override
	public void onLoadResource() {
		background = BitmapFactory.decodeResource(view.getResources(),R.drawable.startback);
	//	titleSnow = createSnow(BitmapFactory.decodeResource(view.getResources(),R.drawable.startsnow));
		startBtn = new MyButton(
				BitmapFactory.decodeResource(view.getResources(),R.drawable.startbtn), 
				BitmapFactory.decodeResource(view.getResources(),R.drawable.startbtndown), 
				(MySystem.srceenWidth-197)/2,MySystem.srceenHeight-54-100){

					@Override
					public void onClick() {
						view.setScene(new TitleScene(view));
					}			
		};

		createBalls();
	}

	public void createBalls() {
		Random random = new Random();

		for (int i = 0; i < 5; i++) {
			int index = random.nextInt(MyRes.getInstance().ballNum);
			int radomX = random.nextInt(MySystem.srceenWidth-200)+100;
			int radomY = random.nextInt(200);
			Ball ball = new Ball(MyRes.getInstance().getBlock()[index],radomX, MySystem.srceenHeight+radomY);
			ball.setAdd(random.nextInt(5)-2, random.nextInt(2)-3);
			balls[i] = ball;
		}
	}
	
	/** 创建转动雪花**/
	public static Bitmap[] createSnow(Bitmap snow) {
		Bitmap[] bitArr = new Bitmap[15];
		for (int i = 0; i < 15; i++) {
			Matrix matrix = new Matrix();
			matrix.setTranslate(snow.getWidth()/2,snow.getHeight()/2);
			matrix.preRotate((int)(45/15*i));

			matrix.preTranslate(-snow.getWidth()/2,-snow.getHeight()/2);
//			matrix.postTranslate((MySystem.srceenWidth-snow.getWidth())/2,(MySystem.srceenHeight-snow.getHeight())/2);

			Paint vPaint = new Paint();
	        vPaint.setStyle( Paint.Style.STROKE );   //画笔样式
	        vPaint.setAlpha((int)(255/15*i));   // Bitmap透明度

	        Bitmap centered = Bitmap.createBitmap(snow.getWidth(),snow.getHeight(),Bitmap.Config.ALPHA_8);
            centered.setDensity(snow.getDensity());
            Canvas canvas = new Canvas(centered);
            canvas.drawBitmap(snow,matrix,vPaint); //通过Canvas绘制Bitmap

			bitArr[i] = centered;
		}
		return bitArr;
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);

		canvas.drawBitmap(background, 0, 0,paint);

	/*	if(snowIndex==15){
			snowIndex = 0;
		}

		Bitmap snow = titleSnow[snowIndex];
		canvas.drawBitmap(snow,(MySystem.srceenWidth- snow.getWidth())/2,(MySystem.srceenHeight- snow.getHeight())/2,paint);	 
		snowIndex++;
		
		if(angle >= 360){ 
			angle = 0;
		}
		angle += 3;

		if(alpha >= 100){
			alphaAdd = -1;
		}
		else if(alpha <=0){
			alphaAdd = 1;
		}
		alpha += alphaAdd;

		if(scale > 5){
			scale = 2f;
		}

		scale = 5f;*/

		for (int i = 0; i < 5; i++) {
			Ball ball = balls[i];
			if(ball.y < -200 && ball.y >-1000){
				ball.getNextY();
				alpha = 0;
			}
			else if(ball.y<-1000){
				createBalls();
			}

			canvas.drawBitmap(ball.bitmap, ball.getNextX(), ball.getNextY(),paint);	 
		}

		startBtn.draw(canvas);

	}



	@Override
	public void onTouchEvent(MotionEvent event) {
		startBtn.onTouch(new Point((int)event.getRawX(),(int)event.getRawY()));
		if(event.getAction() == MotionEvent.ACTION_UP){
			startBtn.isDown = false;
		}

	}

}
